package cn.autumnorange.app.common.provider.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

@Component
@Order(0)
@Slf4j
public class ProviderFilter extends OncePerRequestFilter implements ProviderRequtestParam {
  @Override
  protected void doFilterInternal(
      HttpServletRequest httpServletRequest,
      HttpServletResponse httpServletResponse,
      FilterChain filterChain)
      throws IOException, ServletException {
    long requestStartTime = System.currentTimeMillis();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("请求provider开始时间:" + simpleDateFormat.format(new Date(requestStartTime)));
    httpServletRequest.setAttribute(ProviderRequtestParam.REQUESTSTARTTIME, requestStartTime);
    //    log.info("ProviderFilter exe==========================");

    Enumeration<String> enumeration = httpServletRequest.getHeaderNames();
    while (enumeration.hasMoreElements()) {
      String name = enumeration.nextElement();
      //      log.info("header:" + name + " : " + httpServletRequest.getHeader(name));
    }
    filterChain.doFilter(httpServletRequest, httpServletResponse);
  }

  @Override
  public void setProviderId() {}
}
